package com.tron.config;

import com.tron.interceptor.AuthInterceptor;
import com.tron.interceptor.GlobalInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

@Configuration
public class SpringMvcConfig implements WebMvcConfigurer {

    @Autowired
    @Qualifier("globalInterceptor")
    private GlobalInterceptor globalInterceptor;

    @Autowired
    private AuthInterceptor authInterceptor;

    /**
     * 跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
           .allowedOrigins("*")
           .allowedHeaders("*")
           .allowedMethods("*")
           .allowCredentials(false)
           .maxAge(1800);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 多个拦截器的执行顺序addInterceptor顺序执行
        //放行列表
        List<String> list =new ArrayList<>();
        list.add("/login");
        list.add("/captcha");
        list.add("/swagger-resources/**");
        list.add("/swagger-ui/**");
        list.add("/v3/**");
        list.add("/front/**");
        list.add("/doc.html");
        list.add("/webjars/**");
        list.add("/publicinterface/**");
        list.add("/account/**");
        list.add("/quartz/**");//定时任务管理器
        list.add("/netty/**");//通知管理器
        list.add("/order/**");//订单管理器
        registry.addInterceptor(authInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(list);

    }

    /**
        静态资源放行
        @paramregistry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
        registry.addResourceHandler("/doc.html").addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
